package 枚举;

import sun.misc.Unsafe;
import java.lang.reflect.Field;

/**
 * @author:谢君臣
 * @Date:2021/9/9-11:07
 * @version:1.0
 * @function:
 */
public class 枚举测试 {
    public static void main(String[] args) throws InstantiationException {
        Unsafe unsafe = null;
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            Object o = field.get(null);
            unsafe = (Unsafe) o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        Gander gander = (Gander) unsafe.allocateInstance(Gander.class);
        Gander man = Gander.Man;

        // 枚举不能通过反射获取，但是可以通过Unsafe对象获取

        System.out.println(Gander.Man.getVal());
        System.out.println(Gander.Woman.getVal());
        Resources instance1 = Resources.Single.SINGLE.getInstance();
        Resources instance2 = Resources.Single.SINGLE.getInstance();
        Resources instance3 = Resources.Single.SINGLE.getInstance();
        System.out.println(instance1);
        System.out.println(instance2);
        System.out.println(instance3);
    }
}
enum Gander{
    Man(1),Woman(0);
    private int val;
    Gander(int val) {
        this.val = val;
    }
    public int getVal() {
        return val;
    }
}
class Resources{
    enum Single{
        SINGLE;
        private static final Resources instance = new Resources();
        public Resources getInstance(){
            return instance;
        }
    }
}